import React, { useEffect } from "react"
import { useLocation, useNavigate } from "react-router-dom"
function WithAuth({ children }) {
  function NewAuth() {
    const token = localStorage.getItem("token")
    const navigate = useNavigate()
    const { pathname } = useLocation()
    const withList = ["/login"]
    useEffect(() => {
      if (!token && !withList.includes(pathname)) {
        navigate("/login")
      }
    }, [pathname, token])
    return children
  }
  return <NewAuth />
}

export default WithAuth
